import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_study/widgets/public/app_form_group/widgets/app_input_group.dart';
import 'package:flutter_study/widgets/public/themes/index.dart';
import 'package:flutter_study/widgets/base/app_text_field.dart';
import 'package:rc_widget/rc_widget.dart';

enum AppFormInputType { input, password, select, text }

class AppFormInput extends StatelessWidget {
  const AppFormInput(
      {super.key,
      this.type = AppFormInputType.input,
      this.controller,
      this.hintText,
      this.inputFormatters,
      this.text,
      this.onTap,
      this.suffix,
      this.append});
  final AppFormInputType type;
  final TextEditingController? controller;
  final String? hintText;
  final List<TextInputFormatter>? inputFormatters;
  final String? text;
  final VoidCallback? onTap;
  final String? suffix;
  final Widget? append;

  @override
  Widget build(BuildContext context) {
    switch (type) {
      case AppFormInputType.input:
        return _buildInput();
      case AppFormInputType.password:
        return _buildPassword();
      case AppFormInputType.select:
        return _buildSelect();
      case AppFormInputType.text:
        return _buildText();
      default:
        return const SizedBox.shrink();
    }
  }

  Widget _buildInput() {
    return AppInputGroup(
      suffix: suffix,
      append: append,
      child: AppTextField(
        controller: controller,
        style: AppThemes.of().w400Text128,
        hintText: hintText,
        hintStyle: AppThemes.of().w400Text228,
        inputFormatters: inputFormatters,
      ),
    );
  }

  Widget _buildPassword() {
    return AppInputGroup(
        child: AppFormPassword(
      controller: controller,
      hintText: hintText,
    ));
  }

  Widget _buildSelect() {
    Widget child = Text(
      hintText ?? '',
      style: AppThemes.of().w400Text228,
    );

    if ((text ?? '').isNotEmpty) {
      child = Text(
        text!,
        style: AppThemes.of().w400Text128,
      );
    }

    return AppInputGroup(
      append: Image.asset(
        'assets/images/public/icon-down.webp',
        width: 32.w,
        height: 32.h,
        fit: BoxFit.contain,
      ),
      onTap: onTap,
      child: child,
    );
  }

  Widget _buildText() {
    return AppInputGroup(
      child: Text(
        text ?? '',
        style: AppThemes.of().w400Text128,
      ),
    );
  }
}

class AppFormPassword extends StatefulWidget {
  const AppFormPassword({super.key, this.controller, this.hintText});

  final TextEditingController? controller;
  final String? hintText;

  @override
  State<AppFormPassword> createState() => _AppFormPasswordState();
}

class _AppFormPasswordState extends State<AppFormPassword> {
  bool obscureText = true;

  void switchShow() {
    if (mounted) {
      setState(() {
        obscureText = !obscureText;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [_buildIput(), _buildIcon()],
    );
  }

  Widget _buildIput() {
    return Expanded(
      child: AppTextField(
        obscureText: obscureText,
        controller: widget.controller,
        hintText: widget.hintText,
        style: AppThemes.of().w400Text128,
        hintStyle: AppThemes.of().w400Text228,
        inputFormatters: RcInputFormatters.alphanumeric,
      ),
    );
  }

  Widget _buildIcon() {
    final icon = obscureText ? 'icon-eye' : 'icon-eye-slash';

    return RcImageButton(
      name: 'assets/images/public/$icon.webp',
      dimension: 48.w,
      imageDimension: 32.w,
      margin: EdgeInsets.only(left: 24.w),
      onTap: switchShow,
    );
  }
}
